﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.BusinessLogic;
using DiscoveryLogic.Analytics.BusinessLogic.Entities;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Analytics.DataObjects.Visuals;
using DiscoveryLogic.Analytics.Modules.DataImport;
using DiscoveryLogic.Analytics.Modules.Explorers;
using DiscoveryLogic.Analytics.Modules.ImportRules;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.DataNavigation
{
    

    /// <summary>
    /// Interaction logic for EntitySetsTreeView.xaml
    /// </summary>
    public partial class EntitySetsTreeView : UserControl
    {
        public delegate void OnEntityDataSetSelected(EntityDataSet entitySet);

        public event OnEntityDataSetSelected EntityDataSetSelected;

        private EntitySetsTreePresenter _Presenter;

        public EntitySetsTreeView()
        {
            InitializeComponent();

            //List<EntityDataSet> orgSets = EntityManager.GenerateDataSetNames(EntityType.Organizations, "../images/home.ico");
            //AllEntitySets.Add(new EntitySetsByType(EntityType.Organizations.ToString(), orgSets));
            //List<EntityDataSet> grantSets = EntityManager.GenerateDataSetNames(EntityType.Grants, "../images/main_500.ico");
            //AllEntitySets.Add(new EntitySetsByType(EntityType.Grants.ToString(), grantSets));
            //List<EntityDataSet> pubSets = EntityManager.GenerateDataSetNames(EntityType.Publications, "../images/pub.ico");
            //AllEntitySets.Add(new EntitySetsByType(EntityType.Publications.ToString(), pubSets));
            //List<EntityDataSet> personSets = EntityManager.GenerateDataSetNames(EntityType.Persons, "../images/persons.ico");
            //AllEntitySets.Add(new EntitySetsByType(EntityType.Persons.ToString(), personSets));
            
        }

        public EntitySetsTreeView(EntitySetsTreePresenter presenter):this()
        {
            this._Presenter = presenter;
            this._Presenter.View = this;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        public void PopulateTreeView(ObservableCollection<EntitySetsByType> allEntitySets)
        {
            this.treeEntitySets.ItemsSource = allEntitySets;
        }

        private void treeEntitySets_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if(e.NewValue is EntityDataSet)
            {
                EntityDataSet entitySet = e.NewValue as EntityDataSet;
                if (this.EntityDataSetSelected != null)
                    this.EntityDataSetSelected(entitySet);
            }
        }

        #region explore-> new network
        private void cmu_ExplorePublications_Click(object sender, RoutedEventArgs e)
        {
            if(this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                RuleType ruleType = RuleType.Link;
                EntityDataSet entitySet = (EntityDataSet) this.treeEntitySets.SelectedValue;
                ExploreEntityRelation explorer=new ExploreEntityRelation(entitySet, ruleType);
                PopupWindow dialog=new PopupWindow();
                dialog.Content = explorer;
                dialog.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
                dialog.Title = "Build network";
                // dialog.WindowStyle = WindowStyle.None;
                dialog.ResizeMode = ResizeMode.CanResizeWithGrip;
                dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                dialog.Show();
            }
        }

        private void cmu_ExploreOrganizations_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                RuleType ruleType = RuleType.Link;
                EntityDataSet entitySet = (EntityDataSet)this.treeEntitySets.SelectedValue;
                ExploreEntityRelation explorer = new ExploreEntityRelation(entitySet, ruleType);
                PopupWindow dialog = new PopupWindow();
                dialog.Content = explorer;
                dialog.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
                dialog.Title = "Build network";
                // dialog.WindowStyle = WindowStyle.None;
                dialog.ResizeMode = ResizeMode.CanResizeWithGrip;
                dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                dialog.Show();
            }
        }

        private void cmu_ExplorePersons_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                RuleType ruleType = RuleType.Link;
                EntityDataSet entitySet = (EntityDataSet)this.treeEntitySets.SelectedValue;
                ExploreEntityRelation explorer = new ExploreEntityRelation(entitySet, ruleType);
                PopupWindow dialog = new PopupWindow();
                dialog.Content = explorer;
                dialog.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
                dialog.Title = "Build network";
                // dialog.WindowStyle = WindowStyle.None;
                dialog.ResizeMode = ResizeMode.CanResizeWithGrip;
                dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                dialog.Show();
            }
        }

        private void cmu_ExploreGrants_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                RuleType ruleType = RuleType.Link;
                EntityDataSet entitySet = (EntityDataSet)this.treeEntitySets.SelectedValue;
                ExploreEntityRelation explorer = new ExploreEntityRelation(entitySet, ruleType);
                PopupWindow dialog = new PopupWindow();
                dialog.Content = explorer;
                dialog.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
                dialog.Title = "Build network";
                // dialog.WindowStyle = WindowStyle.None;
                dialog.ResizeMode = ResizeMode.CanResizeWithGrip;
                dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                dialog.Show();
            }
        }

        private void cmu_ExploreDocRelations_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                RuleType ruleType = RuleType.Link;
                EntityDataSet entitySet = (EntityDataSet)this.treeEntitySets.SelectedValue;
                ExploreEntityRelation explorer = new ExploreEntityRelation(entitySet, ruleType);
                PopupWindow dialog = new PopupWindow();
                dialog.Content = explorer;
                dialog.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
                dialog.Title = "Build network";
                // dialog.WindowStyle = WindowStyle.None;
                dialog.ResizeMode = ResizeMode.CanResizeWithGrip;
                dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                dialog.Show();
            }
        }

        private void cmu_Import_Click(object sender, RoutedEventArgs e)
        {
            PopupWindow dialog=new PopupWindow();
            DataImportEditor editor = new DataImportEditor();
            editor.EntityDataSetCreated += new DataImportEditor.OnEntityDataSetCreated(editor_EntityDataSetCreated);
            dialog.Content = editor;
            dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            dialog.SizeToContent = SizeToContent.WidthAndHeight;
            dialog.Title = "Import data";
            dialog.Show();
        }

        private void cmu_AddToNetwork_Click(object sender, RoutedEventArgs e)
        {
            if(this.treeEntitySets.SelectedItem !=null && 
                this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                EntityDataSet entityDataSet = (EntityDataSet) this.treeEntitySets.SelectedValue;
                Network network=new Network(entityDataSet.DataSetName, entityDataSet);
                network.Save();
                MyData.ReadAllNetworks();
            }
        }

        void editor_EntityDataSetCreated(EntityDataSet entitySet)
        {
            entitySet.Save();
            MyData.ReadAllEntitySets();
        }

        private void cmu_OpenDataSet_Click(object sender, RoutedEventArgs e)
        {
            if(this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                EntityDataSet entitySet = (EntityDataSet) this.treeEntitySets.SelectedValue;
                if (this.EntityDataSetSelected != null)
                    this.EntityDataSetSelected(entitySet);
            }
        }

        private void cmu_RenameDataSet_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void cmu_DeleteDataSet_Click(object sender, RoutedEventArgs e)
        {
            if(this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                EntityDataSet entitySet = (EntityDataSet)this.treeEntitySets.SelectedValue;
                DataSetManager.Delete(entitySet);
                MyData.ReadAllEntitySets();
                
            }
        }

        private void ContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            this.cmuExplore.Items.Clear();

            if(this.treeEntitySets.SelectedItem!=null && 
                this.treeEntitySets.SelectedValue is EntityDataSet)
            {
                EntityDataSet entitySet = this.treeEntitySets.SelectedValue as EntityDataSet;
                MenuItem cmuOpen = new MenuItem();
                cmuOpen.Header = "Open data";
                cmuOpen.Tag = "/images/" + EntityIcons.OpenIcon;
                cmuOpen.Click += new RoutedEventHandler(cmu_OpenDataSet_Click);
                this.cmuExplore.Items.Add(cmuOpen);
                MenuItem cmuRename=new MenuItem();
                cmuRename.Header = "Rename data";
                cmuRename.Click +=new RoutedEventHandler(cmu_RenameDataSet_Click);
                this.cmuExplore.Items.Add(cmuRename);
                MenuItem cmuDelete=new MenuItem();
                cmuDelete.Header = "Delete data";
                cmuDelete.Tag = "/images/" + EntityIcons.DeleteIcon;
                cmuDelete.Click +=new RoutedEventHandler(cmu_DeleteDataSet_Click);
                this.cmuExplore.Items.Add(cmuDelete);
                Separator separator=new Separator();
                this.cmuExplore.Items.Add(separator);

                MenuItem cmuExploreGrants=new MenuItem();
                cmuExploreGrants.Header = "-> Grants";
                cmuExploreGrants.Tag = "/images/" + EntityIcons.GrantIcon;
                cmuExploreGrants.Click +=new RoutedEventHandler(cmu_ExploreGrants_Click);
                MenuItem cmuExploreOrgs = new MenuItem();
                cmuExploreOrgs.Header = "-> Orgs";
                cmuExploreOrgs.Tag = "/images/home.png";
                cmuExploreOrgs.Click += new RoutedEventHandler(cmu_ExploreOrganizations_Click);
                MenuItem cmuExplorePersons = new MenuItem();
                cmuExplorePersons.Header = "-> Persons";
                cmuExplorePersons.Tag = "/images/user.png";
                cmuExplorePersons.Click += new RoutedEventHandler(cmu_ExplorePersons_Click);
                MenuItem cmuExplorePubs = new MenuItem();
                cmuExplorePubs.Header = "-> Pubs";
                cmuExplorePubs.Tag = "/images/doc.png";
                cmuExplorePubs.Click += new RoutedEventHandler(cmu_ExplorePublications_Click);
                MenuItem cmuExploreDocs=new MenuItem();
                cmuExploreDocs.Header = "-> Docs";
                cmuExploreDocs.Tag = "/images/doc.png";
                cmuExploreDocs.Click+=new RoutedEventHandler(cmu_ExploreDocRelations_Click);
                MenuItem cmuAddToNetwork=new MenuItem();
                cmuAddToNetwork.Header = "Add to network";
                cmuAddToNetwork.Tag = "/images.add.png";
                cmuAddToNetwork.Click +=new RoutedEventHandler(cmu_AddToNetwork_Click);

                switch(entitySet.EntityType)
                {
                    case EntityType.Grants:
                        this.cmuExplore.Items.Add(cmuExploreOrgs);
                        this.cmuExplore.Items.Add(cmuExplorePersons);
                        this.cmuExplore.Items.Add(cmuExplorePubs);
                        break;
                    case EntityType.Organizations:
                        this.cmuExplore.Items.Add(cmuExploreGrants);
                        this.cmuExplore.Items.Add(cmuExplorePersons);
                        this.cmuExplore.Items.Add(cmuExplorePubs);
                        break;
                    case EntityType.Persons:
                        this.cmuExplore.Items.Add(cmuExploreGrants);
                        this.cmuExplore.Items.Add(cmuExploreOrgs);
                        this.cmuExplore.Items.Add(cmuExplorePubs);
                        break;
                    case EntityType.Publications:
                        this.cmuExplore.Items.Add(cmuExploreGrants);
                        this.cmuExplore.Items.Add(cmuExploreOrgs);
                        this.cmuExplore.Items.Add(cmuExplorePersons);
                        break;
                    case EntityType.Document:
                        this.cmuExplore.Items.Add(cmuExploreDocs);
                        break;
                }

                this.cmuExplore.Items.Add(cmuAddToNetwork);
            }
            else
            {
                MenuItem cmuImport=new MenuItem();
                cmuImport.Header = "Import data";
                cmuImport.Tag = "/images/import.png";
                cmuImport.Click +=new RoutedEventHandler(cmu_Import_Click);
                this.cmuExplore.Items.Add(cmuImport);
            }
        }
        #endregion


    }
}
